package com.shuguoyiyou.wechatofficialaccount.controller;//package com.github.binarywang.demo.wx.mp.controller;

import com.shuguoyiyou.wechatofficialaccount.entity.po.SceneStrTemporaryQR;
import com.shuguoyiyou.wechatofficialaccount.service.RedisService;
import com.shuguoyiyou.wechatofficialaccount.service.impl.IMpQrcodeService;
import com.springboot.cloud.common.core.entity.vo.Result;
import com.springboot.cloud.common.core.redis.RedisCache;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import me.chanjar.weixin.mp.bean.template.WxMpTemplate;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * Created by ZengJian on 2021/2/23 0023 上午 10:01
 */
@AllArgsConstructor
@RestController
@RequestMapping("/oauth/WOA/Qrcode")
public class MpQrcodeController {
    @Autowired
    private RedisService redisService;

    @Autowired
    private IMpQrcodeService iMpQrcodeService;

    private final WxMpService wxMpService;

    @GetMapping("/userInfo")
    public WxMpUser userInfo() throws WxErrorException {
        System.out.println("wxMpUsers:");
        WxMpUser wxMpUser = this.wxMpService.getUserService().userInfo("omd0X6yR16LjrjSjSxTz9NOmriBA");
        System.out.println("wxMpUser:" + wxMpUser);
        return wxMpUser;
    }

    @ApiOperation(value = "换取临时二维码", notes = "换取临时二维码")
    @GetMapping("/qrCodeCreateTmpTicket")
    public Result qrCodeCreateTmpTicket() throws WxErrorException {

        SceneStrTemporaryQR sceneStrTemporaryQR = iMpQrcodeService.qrCodeCreateTmpTicket();

        return Result.success(sceneStrTemporaryQR);
    }

    @GetMapping("/getAllPrivateTemplate")
    public List<WxMpTemplate> getAllPrivateTemplate() throws WxErrorException {

        List<WxMpTemplate> wxMpTemplateList = this.wxMpService.getTemplateMsgService().getAllPrivateTemplate();
        System.out.println("wxMpTemplateList:" + wxMpTemplateList);
        return wxMpTemplateList;
    }

    @GetMapping("/sendTemplateMsg")
    public String sendTemplateMsg() throws WxErrorException {
        WxMpTemplateMessage wxMpTemplateMessage = new WxMpTemplateMessage();
        wxMpTemplateMessage.setToUser("omd0X6yR16LjrjSjSxTz9NOmriBA");
        wxMpTemplateMessage.setTemplateId("xkzA9df5jgOzxnx4QRq40K0MDrPn1PW7dM2htEC8MbY");
        wxMpTemplateMessage.setUrl("http://192.168.1.118:80/oauth/WOA/Qrcode/sendTemplateMsg");
        WxMpTemplateData wxMpTemplateData = new WxMpTemplateData();
        WxMpTemplateData wxMpTemplateData1 = new WxMpTemplateData();
        wxMpTemplateData.setColor("blue");
        wxMpTemplateData.setName("topic");
        wxMpTemplateData.setValue("发货啦");
        wxMpTemplateData1.setColor("yellow");
        wxMpTemplateData1.setName("date");
        wxMpTemplateData1.setValue("2021-2-25");

        wxMpTemplateMessage.addData(wxMpTemplateData);
        wxMpTemplateMessage.addData(wxMpTemplateData1);
        System.out.println("wxMpTemplateMessage:" + wxMpTemplateMessage);

        return this.wxMpService.getTemplateMsgService().sendTemplateMsg(wxMpTemplateMessage);
    }

    @ApiOperation(value = "根据sceneStr获取账号值", notes = "根据sceneStr获取账号值")
    @GetMapping("/getSceneStr/{sceneStr}")
    public Result getSceneStr(@PathVariable("sceneStr") String sceneStr) {
        System.out.println("sceneStr:" + sceneStr);
        String value = redisService.get(sceneStr);
        System.out.println("value:" + value);
        if (!StringUtils.isNotBlank(value)) {
            value = "no";
        }
        return Result.success(value);
    }

    @ApiOperation(value = "根据sceneSStr获取账号值", notes = "根据sceneStr获取账号值")
    @GetMapping("/getSSceneStr")
    public String getSSceneStr(@RequestParam("openId") String openId) {

        String value = redisService.get(openId);
        redisService.remove(openId);
        System.out.println("value:" + value);
        if (!StringUtils.isNotBlank(value)) {
            value = "no";
        }
        redisService.remove(value);
        return value;
    }

}
